package com.morehair.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.morehair.bean.bo.admin.*;
import com.morehair.bean.pojo.Admin;
import com.morehair.bean.pojo.Log;
import com.morehair.bean.pojo.Role;
import com.morehair.bean.pojo.Storage;
import com.morehair.bean.vo.admin.*;
import com.morehair.service.admin.SystemService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

/**
 * Author  kkw
 * Date  2021/6/4 20:57
 * Version 1.0
 * Description :
 */
@RestController
@RequestMapping("admin")
public class SystemController {
    @Autowired
    SystemService systemService;

    @GetMapping("dashboard")
//    @RequiresPermissions("admin")
    public BaseRespVo getDashBoard() {
        DashboardVo dashboardVo=systemService.countAll();
       return BaseRespVo.ok(dashboardVo);
    }

    @GetMapping("admin/list")
    @RequiresPermissions("admin:admin:list")
    public BaseRespVo getAdminList(AdminBo adminBo) {
        TotalAndItemsVo<Admin> items=systemService.selecetAdminsByBo(adminBo);
        return BaseRespVo.ok(items);
    }

    @GetMapping("role/options")
    @RequiresPermissions("admin:role:read")
    public BaseRespVo getOptions() {
        List<OptionVo> optionVos =systemService.selectAllRole();
        return BaseRespVo.ok(optionVos);
    }

    @PostMapping("admin/create")
    @RequiresPermissions("admin:admin:create")
    public BaseRespVo creatAdmin(@RequestBody Admin admin) {
        Admin admin1 = null;
        try {
            admin1 = systemService.creatAdmin(admin);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (admin1 == null) {
            return BaseRespVo.fail(602,admin1,"管理员已经存在");
        }
        return BaseRespVo.ok(admin1);
    }

    @PostMapping("admin/update")
    @RequiresPermissions("admin:admin:update")
    public BaseRespVo updateAdmin(@RequestBody Admin admin) throws NoSuchAlgorithmException {
//        Admin admin1= null;
        Admin admin1 = systemService.updateAdmin(admin);
        if (admin1 == null) {
            return BaseRespVo.fail(602,admin1,"管理员已经存在,请你改成其他名字");
        }
        return BaseRespVo.ok(admin1);
    }

    @PostMapping("admin/delete")
    @RequiresPermissions("admin:admin:delete")
    public BaseRespVo deleteAdmin(@RequestBody Admin admin){
      boolean b=systemService.deleteAdmin(admin);
      if (b) {
          return BaseRespVo.ok();
      }else {
          return BaseRespVo.fail(604,null,"管理员不能删除自己的账号");
      }
    }

    @GetMapping("role/list")
    @RequiresPermissions("admin:role:list")
    public BaseRespVo roles(RoleBo roleBo){
        TotalAndItemsVo<Role> totalAndItemsVo=systemService.selectAllRoleBypage(roleBo);
        return BaseRespVo.ok(totalAndItemsVo);
    }

    @PostMapping("role/create")
    @RequiresPermissions("admin:role:create")
    public BaseRespVo deleteAdmin1(@RequestBody Map<String,String> map){
        Role role=systemService.insertRole(map);
        if (role == null) {
            return BaseRespVo.fail(602,role,"该角色已经存在");
        }
        return BaseRespVo.ok(role);
    }

    @PostMapping("role/update")
    @RequiresPermissions("admin:role:update")
    public BaseRespVo updateRole(@RequestBody Role role) {
        Role role1=systemService.updateRole(role);
        if (role1 == null) {
            return BaseRespVo.fail(602,role1,"角色已经存在,请你改成其他名字");
        }
        return BaseRespVo.ok(role1);
    }

    @PostMapping("role/delete")
    @RequiresPermissions("admin:role:delete")
    public BaseRespVo deleteRole(@RequestBody Role role) throws JsonProcessingException {
       boolean b= systemService.deleteRole(role);
        if (b) {
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail(642,null,"当前角色存在管理员,不能删除");
        }
    }

    @GetMapping("log/list")
    @RequiresPermissions("admin:log:list")
    public BaseRespVo getLogList(LogBo logBo) {
        TotalAndItemsVo<Log> totalAndItemsVo=systemService.selectLogsByPage(logBo);
        return BaseRespVo.ok(totalAndItemsVo);
    }

    @GetMapping("role/permissions")
    @RequiresPermissions("admin:role:permission:get")

    public BaseRespVo getPermission(Integer roleId) {
        PermissionVo permissionVo=systemService.getPermission(roleId);
        return BaseRespVo.ok(permissionVo);
    }

    @PostMapping("role/permissions")
    @RequiresPermissions("admin:role:permission:update")
    public BaseRespVo changePermission(@RequestBody RolePermissionBo rolePermissionBo) {
        boolean b=systemService.changePermission(rolePermissionBo);
        if (b) {
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail(641,null,"当前角色权限不能变更");
        }
    }

    @GetMapping("storage/list")
    @RequiresPermissions("admin:storage:list")
    public BaseRespVo storageList(StorageListBo storageListBo) {
        TotalAndItemsVo<Storage> totalAndItemsVo=systemService.getStorage(storageListBo);
        return BaseRespVo.ok(totalAndItemsVo);
    }

}
